$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("packMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });


    $(".head .icon-lishi1").on("click", function () {
        window.location.href = "heatwareMerge_lishi.html";
    });

    $("#goods_id").focus();

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描热处理产品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            if ($(".content .mergeList_ul li").length > 0) {
                $alert("已添加合并产品，不可扫描原料");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length == 1) {
                $alert("不可重复扫描");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return
            }
            $(this).val("");
            var onebatch = "";
            if ($(".content .goodsList_ul li").length > 0) {
                onebatch = $(".content .goodsList_ul li").eq("0").find("span.goods_cpph").text();
            }
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/heat/getWarehouseForMerge',
                data: {
                    batch: val,
                    onebatch: onebatch
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + data.batch + "']").length == 1) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                pass = 1;
                                $("#goods_id").val("").focus();
                            }, 1400);
                            return
                        }

                        var data_html = '<li attr-data="' + data.batch + '"><div class="row no-gutter">' +
                            '<div class="col-90">' +
                            '<p class="p_none">' +
                            '<span class="goods_houseCode">' + data.warehouseCode + '</span>' +
                            '</p><p><span>编号:</span><span class="sp goods_cpph">' +
                            data.batch + '</span></p><p><span>排产单:</span><span class="sp goods_cppcd">' +
                            (data.workplanNumber || "") + '</span></p><p><span>名称:</span><span class="sp goods_cppcd">' +
                            (data.inventoryName || "") + '</span><span>规格:</span><span class="sp goods_cpgg">' +
                            data.inventorySpecification + '</span></p><p><span>净重:</span><span class="sp goods_cpzl">' +
                            data.availableQuantity + '</span><span>毛重:</span><span class="sp goods_cpmz">' +
                            data.grossQuantity + '</span></p></div>' +
                            '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                        $(".content .list-block .goodsList_ul").prepend(data_html);
                        $alert("获取成功", 800);
                        badge_text();
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条

    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        if ($(".content .mergeList_ul li").length > 0) {
            return $alert("已添加合并产品，不可删除原料");
        }
        var jianhao = $(this);
        $.confirm('确定删除此原料？', function () {
            jianhao.parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    $(".content").on("click", ".mergeList_ul li .col-10", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 获取编号
    $(".foot").on("click", ".add_go", function () {
        if ($(".content .goodsList_ul li").length == 0) {
            $alert("请先扫描合并的原料！");
            $("#goods_id").val("").focus();
            return
        }
        if (Number($(".details span.sy_weight").text()) <= 0) {
            $alert("剩余重量不足了！");
            $("#goods_id").val("").focus();
            return
        }
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/heat/getHeatMergenumber',
            data: {},
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    $(".mengceng").show();
                    $("div.div_pt").show();
                    $("div.div_pt input").val("");
                    $("div.div_pt .batch").val(res.result.batch);
                    $("div.div_pt .weight").focus();
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                $alert(error.responseText);
            }
        });
    });
    //点击  取消
    $("div.div_pt .pt_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_pt").hide();
        $("#goods_id").focus();
    });
    //点击  取消
    $("div.div_pt .pt_queding").click(function () {
        if (!$("div.div_pt .batch").val()) {
            return $alert("编号不可为空！");
        } else if (!$("div.div_pt .weight").val()) {
            $alert("重量不可为空！");
            $("div.div_pt .weight").val();
            return
        } else if (Number($("div.div_pt .weight").val()) <= 0) {
            $alert("重量不可小于0！");
            $("div.div_pt .weight").val();
            return
        } else if (Number($("div.div_pt .weight").val()) > Number($(".details span.sy_weight").text())) {
            $alert("重量不可大于剩余重量" + Number($(".details span.sy_weight").text()));
            $("div.div_pt .weight").val();
            return
        } else {
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/heat/checkHeatMergenumber',
                data: {
                    batch: $("div.div_pt .batch").val(),
                    weight: $("div.div_pt .weight").val()
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        var data = res.result;
                        var li_con = '<li attr-data="' + data.batch + '"><div class="row no-gutter"><div class="col-90">' +
                            '<p><span>编号：</span><span class="sp batch">' + $("div.div_pt .batch").val() + '</span></p>' +
                            '<p><span>重量：</span><span class="sp weight">' + $("div.div_pt .weight").val() + '</span>T</p>' +
                            '</div><div class="col-10"><span class="icon iconfont icon-msnui-jian"></span>';
                        li_con += '</div></div></li>';
                        $(".content .list-block .mergeList_ul").prepend(li_con);
                        badge_text();
                        $alert("添加成功", 800);
                        $(".mengceng").hide();
                        $("div.div_pt").hide();
                        $("#goods_id").val("").focus();
                    } else {
                        pass = 1;
                        console.log(res);
                        setTimeout(function () {
                            $("div.div_pt input.cpkw").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    setTimeout(function () {
                        $("div.div_pt input.cpkw").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // 点击 添加入库
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请扫描合并的原料！");
        }
        if ($(".content .mergeList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请获取合并的产品！");
        }
        $.confirm($(".content .mergeList_ul li").length + '件，确定合并吗？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var batchs = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                batchs.push($(".content .goodsList_ul li").eq(i).find("span.goods_cpph").text());
            }
            var mergeDatas = [];
            for (var i = 0; i < $(".content .mergeList_ul li").length; i++) {
                mergeDatas.push({
                    batch: $(".content .mergeList_ul li").eq(i).find("span.batch").text(),
                    weight: $(".content .mergeList_ul li").eq(i).find("span.weight").text()
                });
            }
            loading_show("提交中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/heat/heatwareMerge",
                data: {
                    batchs: JSON.stringify(batchs),
                    mergeDatas: JSON.stringify(mergeDatas)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        // var data = res.result;
                        console.log("产品合并成功");
                        $alert("合并成功！")
                        setTimeout(function () {
                            window.location.href = "heatwareMerge.html";
                        }, 1400);
                        // 打印 热处理 收线 标签
                        if (res.result) {
                            for (var i = 0; i < res.result.length; i++) {
                                setTimeout(function () {
                                    takeupPrint("heatTakeup", res.result[i]);
                                }, i * 1000);
                            }
                        }
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass =1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpzl").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(Number(parseFloat(allWeight).toFixed(3)));

        var merge_len = $(".content .mergeList_ul li").length;
        var mergeWeight = 0;
        for (var i = 0; i < merge_len; i++) {
            var weight = $(".content .mergeList_ul li").eq(i).find(".row span.weight").html();
            mergeWeight = accAdd(mergeWeight, weight);
        }
        $(".details span.merge_length").html(merge_len);
        $(".details span.merge_weight").html(Number(parseFloat(mergeWeight).toFixed(3)));
        $(".details span.sy_weight").html(Number(parseFloat(accSub(allWeight, mergeWeight)).toFixed(3)));
    }

    badge_text();

});